using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Models.Ledger.Merchconf;

namespace JlPay.Sdk.Services.Ledger.Merchconf
{
    /// <summary>
    /// 商户配置服务类
    /// </summary>
    public class MerchconfService : BaseService
    {
        /// <summary>
        /// 创建商户配置服务
        /// </summary>
        /// <param name="config">配置对象</param>
        public MerchconfService(Config config) : base(config)
        {
        }

        /// <summary>
        /// 获取API URL
        /// </summary>
        /// <param name="requestType">请求类型</param>
        /// <returns>API URL</returns>
        private string GetApiUrl(Type requestType)
        {
            var apiPrefix = requestType.GetField("ApiPrefix")?.GetValue(null)?.ToString();
            var apiUri = requestType.GetField("ApiUri")?.GetValue(null)?.ToString();
            
            if (!string.IsNullOrEmpty(apiPrefix) && !string.IsNullOrEmpty(apiUri))
            {
                return "/" + apiPrefix.TrimStart('/') + apiUri;
            }
            
            return "/open/ledger/merchconf";
        }

        /// <summary>
        /// 查询商户分账信息
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevQueryResponse> QueryMerchRevAsync(MerchRevQueryRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevQueryRequest));
            return await PostAsync<MerchRevQueryRequest, MerchRevQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 解绑商户分账
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevUnbindResponse> UnbindMerchRevAsync(MerchRevUnbindRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevUnbindRequest));
            return await PostAsync<MerchRevUnbindRequest, MerchRevUnbindResponse>(request, apiPath);
        }

        /// <summary>
        /// 绑定商户分账
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevBindResponse> BindMerchRevAsync(MerchRevBindRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevBindRequest));
            return await PostAsync<MerchRevBindRequest, MerchRevBindResponse>(request, apiPath);
        }

        /// <summary>
        /// 查询商户分账绑定
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevBindQueryResponse> QueryMerchRevBindAsync(MerchRevBindQueryRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevBindQueryRequest));
            return await PostAsync<MerchRevBindQueryRequest, MerchRevBindQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 开通商户分账
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevOpenResponse> OpenMerchRevAsync(MerchRevOpenRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevOpenRequest));
            return await PostAsync<MerchRevOpenRequest, MerchRevOpenResponse>(request, apiPath);
        }

        /// <summary>
        /// 更新商户分账结算
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchRevSettleUpdateResponse> UpdateMerchRevSettleAsync(MerchRevSettleUpdateRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchRevSettleUpdateRequest));
            return await PostAsync<MerchRevSettleUpdateRequest, MerchRevSettleUpdateResponse>(request, apiPath);
        }

        /// <summary>
        /// 开通商户分账发行
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchIssueOpenResponse> OpenMerchIssueAsync(MerchIssueOpenRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchIssueOpenRequest));
            return await PostAsync<MerchIssueOpenRequest, MerchIssueOpenResponse>(request, apiPath);
        }

        /// <summary>
        /// 查询商户分账发行
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchIssueQueryResponse> QueryMerchIssueAsync(MerchIssueQueryRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchIssueQueryRequest));
            return await PostAsync<MerchIssueQueryRequest, MerchIssueQueryResponse>(request, apiPath);
        }

        /// <summary>
        /// 更新商户分账发行
        /// </summary>
        /// <param name="request">请求对象</param>
        /// <returns>响应对象</returns>
        public async Task<MerchIssueUpdateResponse> UpdateMerchIssueAsync(MerchIssueUpdateRequest request)
        {
            var apiPath = GetApiUrl(typeof(MerchIssueUpdateRequest));
            return await PostAsync<MerchIssueUpdateRequest, MerchIssueUpdateResponse>(request, apiPath);
        }
    }
} 